package com.edu.cold.druid.common;

import java.sql.*;
import java.util.Properties;

/**
 * <p>
 * Druid帮助类
 * </p>
 *
 * @author jpge
 * @since 2024-04-05
 */
public class DruidHelper {

    private final String url = "jdbc:avatica:remote:url=http://172.17.0.143:8888/druid/v2/sql/avatica/";
    private final Properties conf = new Properties();
    private Connection connection;


    /**
     * 获得Druid连接
     */
    public Connection getConnection() {
        try {
            if (null == connection) {
                connection = DriverManager.getConnection(url, conf);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭Druid连接
     */
    public void close(Connection connection, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
